package com.example.applications.Kotlin

fun main() {
    //with
    val car1 = vehicle()
    with(car1) {
        name = "汽车1"
        speed = 121
        run()
        stop()
    }

    println("品牌" + car1.name + "," + "时速" + car1.speed)

    //apply
    val car2 = vehicle().apply {
        name = "汽车2"
        speed = 122
        run()
        stop()
    }
    println("品牌" + car2.name + "," + "时速" + car2.speed)

    //let
    var dreamCar: vehicle? = null
    dreamCar?.let {
        print(dreamCar.name) //不存在，所有不执行
    }

    //run创建局部作用域
    run {
        val car = vehicle()
        car.run {
            name = "汽车4"
            speed = 124
            run()
            stop()
        }
        println(car.name)
    }

}


class vehicle {
    var name: String = "小汽车"
    var speed: Int = 0
    fun run() {
        println(name + "正在加速")
    }

    fun stop() {
        println(name + "正在减速")
    }
}